﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ElfTyping.Animations;
using Microsoft.Xna.Framework;

namespace ElfTyping.General
{
    public class TextBox
    {
        public static int Top = 425;

        public string Text { get; set; }
        public Animation Portrait { private get; set; }
        public string Name { get; set; }

        static Animation TextBoxBackground = new Animation(@"Textures\Text_Window_CT", new Rectangle(8, 8, 255, 79));
        static Rectangle TextBoxRectangle = new Rectangle(50, Top, 700, 158);

        static Animation NameBackground = new Animation(@"Textures\Text_Window_CT", new Rectangle(8, 624, 255, 79));

        static Rectangle NameLeftRectangle = new Rectangle(80, Top - 15, 108, 30);
        static Rectangle NameRightRectangle = new Rectangle(612, Top - 15, 108, 30);
        private Rectangle nameRectangle;

        static Vector2 NameLeftCenter = new Vector2(134, Top);
        static Vector2 NameRightCenter = new Vector2(666, Top);
        private Vector2 nameCenter;

        Point PortraitLeftCenter = new Point(140, 504);
        Point PortraitRightCenter = new Point(660, 504);
        private Point portraitCenter;

        static Vector2 TextLeftPosition = new Vector2(225, 445);
        static Vector2 TextRightPosition = new Vector2(70, 445);
        private Vector2 textPosition;

        private bool _isPortraitOnLeft;
        public bool IsPortraitOnLeft
        {
            get { return _isPortraitOnLeft; }
            set
            {
                _isPortraitOnLeft = value;

                portraitCenter = value ? PortraitLeftCenter : PortraitRightCenter;
                textPosition = value ? TextLeftPosition : TextRightPosition;
                nameRectangle = value ? NameLeftRectangle : NameRightRectangle;
                nameCenter = value ? NameLeftCenter : NameRightCenter;

                if (Portrait != null)
                {
                    Portrait.FlippedHorizontally = value;
                }

            }
        }

        public TextBox() : this("") { }

        public TextBox(string text)
        {
            Text = text;
            Portrait = null;
            IsPortraitOnLeft = true;
        }

        public void Tick(float dt)
        {
            TextBoxBackground.Advance(dt);

            if (Portrait != null)
            {
                Portrait.Advance(dt);
            }
        }

        public void Draw(Camera camera)
        {
            if (Text == null)
            {
                return;
            }

            TextBoxBackground.Draw(camera, TextBoxRectangle);
            camera.MakeText(Text)
                .WithFont(Camera.TextDrawer.TimesNewRomanBigFont)
                .WithColor(Color.White)
                .WithTopLeftPosition(textPosition)
                .Draw();

            if (Portrait != null)
            {
                if (IsPortraitOnLeft)
                {
                    Portrait.FlippedHorizontally = true;
                }

                Portrait.BlitCenteredAt(camera, portraitCenter);
            }

            if (!string.IsNullOrEmpty(Name))
            {
                NameBackground.Draw(camera, nameRectangle);
                camera.MakeText(Name)
                    .WithCenteredPosition(nameCenter)
                    .WithColor(Color.White)
                    .Draw();
            }
        }
    }
}
